home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch8 / PlayTmr.frm < prev    next >
Text File  |  1999-06-12  |  9KB  |  339 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
  3. Begin VB.Form frmPlayTmr 
  4.    Caption         =   "PlayTmr"
  5.    ClientHeight    =   3825
  6.    ClientLeft      =   1680
  7.    ClientTop       =   975
  8.    ClientWidth     =   5850
  9.    LinkTopic       =   "Form1"
  10.    PaletteMode     =   1  'UseZOrder
  11.    ScaleHeight     =   255
  12.    ScaleMode       =   3  'Pixel
  13.    ScaleWidth      =   390
  14.    Begin VB.Timer tmrFrame 
  15.       Enabled         =   0   'False
  16.       Interval        =   100
  17.       Left            =   120
  18.       Top             =   1920
  19.    End
  20.    Begin VB.TextBox txtNumFrames 
  21.       Height          =   285
  22.       Left            =   1560
  23.       TabIndex        =   10
  24.       Text            =   "100"
  25.       Top             =   120
  26.       Width           =   375
  27.    End
  28.    Begin VB.OptionButton optRunType 
  29.       Caption         =   "Looping"
  30.       Height          =   255
  31.       Index           =   2
  32.       Left            =   360
  33.       TabIndex        =   8
  34.       Top             =   1560
  35.       Width           =   1095
  36.    End
  37.    Begin VB.OptionButton optRunType 
  38.       Caption         =   "Reversing"
  39.       Height          =   255
  40.       Index           =   1
  41.       Left            =   360
  42.       TabIndex        =   7
  43.       Top             =   1200
  44.       Width           =   1095
  45.    End
  46.    Begin VB.OptionButton optRunType 
  47.       Caption         =   "One time"
  48.       Height          =   255
  49.       Index           =   0
  50.       Left            =   360
  51.       TabIndex        =   6
  52.       Top             =   840
  53.       Value           =   -1  'True
  54.       Width           =   1095
  55.    End
  56.    Begin VB.TextBox txtFramesPerSecond 
  57.       Height          =   285
  58.       Left            =   1560
  59.       TabIndex        =   5
  60.       Text            =   "20"
  61.       Top             =   480
  62.       Width           =   375
  63.    End
  64.    Begin VB.PictureBox picFrame 
  65.       AutoRedraw      =   -1  'True
  66.       AutoSize        =   -1  'True
  67.       Height          =   375
  68.       Index           =   0
  69.       Left            =   1560
  70.       ScaleHeight     =   21
  71.       ScaleMode       =   3  'Pixel
  72.       ScaleWidth      =   21
  73.       TabIndex        =   2
  74.       Top             =   1560
  75.       Visible         =   0   'False
  76.       Width           =   375
  77.    End
  78.    Begin VB.CommandButton cmdStart 
  79.       Caption         =   "Start"
  80.       Default         =   -1  'True
  81.       Enabled         =   0   'False
  82.       Height          =   375
  83.       Left            =   600
  84.       TabIndex        =   1
  85.       Top             =   2040
  86.       Width           =   855
  87.    End
  88.    Begin VB.PictureBox picCanvas 
  89.       Height          =   3810
  90.       Left            =   2040
  91.       ScaleHeight     =   250
  92.       ScaleMode       =   3  'Pixel
  93.       ScaleWidth      =   250
  94.       TabIndex        =   0
  95.       Top             =   0
  96.       Width           =   3810
  97.    End
  98.    Begin MSComDlg.CommonDialog dlgOpenFile 
  99.       Left            =   1560
  100.       Top             =   960
  101.       _ExtentX        =   847
  102.       _ExtentY        =   847
  103.       _Version        =   393216
  104.       CancelError     =   -1  'True
  105.    End
  106.    Begin VB.Label Label2 
  107.       Caption         =   "Frames to load:"
  108.       Height          =   255
  109.       Left            =   120
  110.       TabIndex        =   9
  111.       Top             =   120
  112.       Width           =   1455
  113.    End
  114.    Begin VB.Label Label1 
  115.       Caption         =   "Frames per second:"
  116.       Height          =   255
  117.       Index           =   1
  118.       Left            =   120
  119.       TabIndex        =   4
  120.       Top             =   480
  121.       Width           =   1455
  122.    End
  123.    Begin VB.Label lblResults 
  124.       Height          =   615
  125.       Left            =   120
  126.       TabIndex        =   3
  127.       Top             =   2640
  128.       Width           =   1815
  129.    End
  130.    Begin VB.Menu mnuFile 
  131.       Caption         =   "&File"
  132.       Begin VB.Menu mnuFileOpen 
  133.          Caption         =   "&Open..."
  134.          Shortcut        =   ^O
  135.       End
  136.    End
  137. End
  138. Attribute VB_Name = "frmPlayTmr"
  139. Attribute VB_GlobalNameSpace = False
  140. Attribute VB_Creatable = False
  141. Attribute VB_PredeclaredId = True
  142. Attribute VB_Exposed = False
  143. Option Explicit
  144.  
  145. Private Enum RunTypes
  146.     run_OneTime
  147.     run_BackAndForth
  148.     run_Looping
  149. End Enum
  150.  
  151. Private NumImages As Integer
  152. Private MaxImage As Integer
  153. Private NextImage As Integer
  154. Private RunType As RunTypes
  155. Private RunForward As Integer
  156.  
  157. Private StartTime As Long
  158. Private StopTime As Long
  159. Private NumPlayed As Integer
  160.  
  161. ' Load the images.
  162. Private Sub LoadImages(file_name As String)
  163. Dim base As String
  164. Dim i As Integer
  165.  
  166.     ' Get the base file name.
  167.     base = Left$(file_name, Len(file_name) - 5)
  168.  
  169.     ' See how many frames the user wants to load.
  170.     If Not IsNumeric(txtNumFrames.Text) Then _
  171.         txtNumFrames.Text = Format$(10)
  172.     NumImages = CInt(txtNumFrames.Text)
  173.  
  174.     ' Create any needed picture boxes.
  175.     For i = MaxImage + 1 To NumImages - 1
  176.         Load picFrame(i)
  177.     Next i
  178.  
  179.     ' Get rid of any that are no longer needed.
  180.     For i = NumImages To MaxImage
  181.         Unload picFrame(i)
  182.     Next i
  183.     MaxImage = NumImages - 1
  184.     
  185.     ' Load the images.
  186.     On Error GoTo LoadPictureError
  187.     i = 0
  188.     Do While i < NumImages
  189.         lblResults.Caption = Format$(i + 1)
  190.         lblResults.Refresh
  191.         picFrame(i).Picture = LoadPicture(base & Format$(i) & ".bmp")
  192.         i = i + 1
  193.     Loop
  194.  
  195.     picCanvas.AutoSize = True
  196.     picCanvas.Picture = picFrame(0).Image
  197.     picCanvas.AutoSize = False
  198.     lblResults.Caption = ""
  199.     txtNumFrames.Text = Format$(NumImages)
  200.     Exit Sub
  201.     
  202. LoadPictureError:
  203.     ' We ran out of images early.
  204.     NumImages = i
  205.     txtNumFrames.Text = Format$(NumImages)
  206.     Resume Next
  207. End Sub
  208.  
  209. ' Start or stop playing.
  210. Private Sub CmdStart_Click()
  211.     If tmrFrame.Enabled Then
  212.         ' Stop the animation.
  213.         StopAnimation
  214.     Else
  215.         ' Start the animation.
  216.         StartAnimation
  217.     End If
  218. End Sub
  219. ' Start playing.
  220. Private Sub StartAnimation()
  221. Dim i As Integer
  222.  
  223.     ' Start the animation.
  224.     cmdStart.Caption = "Stop"
  225.     lblResults.Caption = ""
  226.     NumPlayed = 0
  227.     NextImage = 0
  228.     RunForward = True
  229.  
  230.     ' See what kind of run it is (looping, etc.).
  231.     For i = 0 To 2
  232.         If optRunType(i).Value Then Exit For
  233.     Next i
  234.     RunType = i
  235.  
  236.     ' See how long it should be between frames.
  237.     If Not IsNumeric(txtFramesPerSecond.Text) Then _
  238.         txtFramesPerSecond.Text = "20"
  239.     tmrFrame.Interval = 1000 / CInt(txtFramesPerSecond.Text)
  240.  
  241.     ' Start the timer.
  242.     StartTime = GetTickCount
  243.     tmrFrame.Enabled = True
  244. End Sub
  245.  
  246. ' Stop playing.
  247. Private Sub StopAnimation()
  248. Dim StopTime As Long
  249.  
  250.     ' Stop the animation.
  251.     StopTime = GetTickCount
  252.     tmrFrame.Enabled = False
  253.     cmdStart.Caption = "Start"
  254.  
  255.     lblResults.Caption = _
  256.         Format$(NumPlayed) & " frames/" & _
  257.         Format$((StopTime - StartTime) / 1000#, "0.00") & _
  258.         " sec" & vbCrLf & vbCrLf & _
  259.         Format$(CSng(NumPlayed) / ((StopTime - StartTime) / 1000#), "0.00") & _
  260.         " frames/sec"
  261. End Sub
  262.  
  263. Private Sub Form_Load()
  264.     dlgOpenFile.InitDir = App.Path
  265. End Sub
  266.  
  267. ' Load new image files.
  268. Private Sub mnuFileOpen_Click()
  269. Dim file_name As String
  270.  
  271.     ' Let the user select a file.
  272.     On Error Resume Next
  273.     dlgOpenFile.FileName = "*_0.BMP"
  274.     dlgOpenFile.Flags = cdlOFNFileMustExist + cdlOFNHideReadOnly
  275.     dlgOpenFile.ShowOpen
  276.     If Err.Number = cdlCancel Then
  277.         Exit Sub
  278.     ElseIf Err.Number <> 0 Then
  279.         Beep
  280.         MsgBox "Error selecting file.", , vbExclamation
  281.         Exit Sub
  282.     End If
  283.     On Error GoTo 0
  284.  
  285.     Screen.MousePointer = vbHourglass
  286.     DoEvents
  287.  
  288.     file_name = Trim$(dlgOpenFile.FileName)
  289.     dlgOpenFile.InitDir = Left$(file_name, Len(file_name) _
  290.         - Len(dlgOpenFile.FileTitle) - 1)
  291.     Caption = "PlayTmr [" & dlgOpenFile.FileTitle & "]"
  292.  
  293.     ' Load the pictures.
  294.     On Error GoTo LoadError
  295.     LoadImages file_name
  296.     On Error GoTo 0
  297.  
  298.     cmdStart.Enabled = True
  299.     Screen.MousePointer = vbDefault
  300.     Exit Sub
  301.  
  302. LoadError:
  303.     Screen.MousePointer = vbDefault
  304.     MsgBox "Error " & Format$(Err.Number) & _
  305.         " opening file '" & file_name & "'" & vbCrLf & _
  306.         Err.Description
  307. End Sub
  308.  
  309. ' Display the next frame.
  310. Private Sub tmrFrame_Timer()
  311.     ' Display the next frame.
  312.     picCanvas.Picture = picFrame(NextImage).Image
  313.     NumPlayed = NumPlayed + 1
  314.  
  315.     ' See which frame comes next.
  316.     If RunForward Then
  317.         ' Display the next frame next.
  318.         NextImage = NextImage + 1
  319.         If NextImage >= NumImages Then
  320.             Select Case RunType
  321.                 Case run_OneTime
  322.                     StopAnimation
  323.                 Case run_BackAndForth
  324.                     RunForward = False
  325.                     NextImage = NumImages - 2
  326.                 Case run_Looping
  327.                     NextImage = 0
  328.             End Select
  329.         End If
  330.     Else
  331.         ' Display the previous frame next.
  332.         NextImage = NextImage - 1
  333.         If NextImage < 0 Then
  334.             RunForward = True
  335.             NextImage = 1
  336.         End If
  337.     End If
  338. End Sub
  339.